// 移动端登录注册逻辑

// Toast提示
function showToast(message, duration = 2000) {
    const toast = document.createElement('div');
    toast.className = 'toast';
    toast.textContent = message;
    document.body.appendChild(toast);

    setTimeout(() => {
        toast.style.animation = 'fadeOut 0.3s';
        setTimeout(() => toast.remove(), 300);
    }, duration);
}

// 登录表单处理
const loginForm = document.getElementById('loginForm');
if (loginForm) {
    loginForm.addEventListener('submit', async (e) => {
        e.preventDefault();

        const username = document.getElementById('username').value.trim();
        const password = document.getElementById('password').value;

        if (!username || !password) {
            showToast('请填写所有字段');
            return;
        }

        const loginBtn = document.getElementById('loginBtn');
        const loginBtnText = document.getElementById('loginBtnText');
        const loginSpinner = document.getElementById('loginSpinner');

        // 显示加载状态
        loginBtn.disabled = true;
        loginBtnText.style.display = 'none';
        loginSpinner.style.display = 'block';

        try {
            const response = await fetch('/api/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ username, password })
            });

            const data = await response.json();

            if (data.success) {
                showToast('登录成功！');
                setTimeout(() => {
                    window.location.href = '/';
                }, 1000);
            } else {
                showToast(data.message || '登录失败，请重试');
                loginBtn.disabled = false;
                loginBtnText.style.display = 'block';
                loginSpinner.style.display = 'none';
            }
        } catch (error) {
            console.error('登录错误:', error);
            showToast('网络错误，请重试');
            loginBtn.disabled = false;
            loginBtnText.style.display = 'block';
            loginSpinner.style.display = 'none';
        }
    });
}

// 注册表单处理
const registerForm = document.getElementById('registerForm');
if (registerForm) {
    registerForm.addEventListener('submit', async (e) => {
        e.preventDefault();

        const username = document.getElementById('username').value.trim();
        const email = document.getElementById('email').value.trim();
        const password = document.getElementById('password').value;
        const confirmPassword = document.getElementById('confirmPassword').value;

        // 验证
        if (!username || !email || !password || !confirmPassword) {
            showToast('请填写所有字段');
            return;
        }

        if (password.length < 6) {
            showToast('密码长度至少为6位');
            return;
        }

        if (password !== confirmPassword) {
            showToast('两次输入的密码不一致');
            return;
        }

        const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
        if (!emailRegex.test(email)) {
            showToast('请输入有效的邮箱地址');
            return;
        }

        const registerBtn = document.getElementById('registerBtn');
        const registerBtnText = document.getElementById('registerBtnText');
        const registerSpinner = document.getElementById('registerSpinner');

        // 显示加载状态
        registerBtn.disabled = true;
        registerBtnText.style.display = 'none';
        registerSpinner.style.display = 'block';

        try {
            const response = await fetch('/api/register', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ username, email, password })
            });

            const data = await response.json();

            if (data.success) {
                showToast('注册成功！正在跳转...');
                setTimeout(() => {
                    window.location.href = '/login';
                }, 1000);
            } else {
                showToast(data.message || '注册失败，请重试');
                registerBtn.disabled = false;
                registerBtnText.style.display = 'block';
                registerSpinner.style.display = 'none';
            }
        } catch (error) {
            console.error('注册错误:', error);
            showToast('网络错误，请重试');
            registerBtn.disabled = false;
            registerBtnText.style.display = 'block';
            registerSpinner.style.display = 'none';
        }
    });
}

// 添加fadeOut动画
const style = document.createElement('style');
style.textContent = `
    @keyframes fadeOut {
        from { opacity: 1; }
        to { opacity: 0; }
    }
`;
document.head.appendChild(style);

